package com.caigou.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.caigou.bean.cms.entity.Comment;
import com.caigou.bean.cms.param.CollectionLikesParam;
import com.caigou.bean.exam.entity.Users;
import com.caigou.bean.vo.ArticleCollectionLikesVO;
import com.caigou.bean.vo.CollectionLikesVO;
import com.caigou.enums.Constant;
import com.caigou.enums.ResponseEnum;
import com.caigou.mapper.cms.ArticleMapper;
import com.caigou.mapper.cms.CommentMapper;
import com.caigou.pojo.ArticleList;
import com.caigou.service.ICollectionLikesService;
import com.caigou.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class CollectionLikesServiceImpl implements ICollectionLikesService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private CommentMapper commentMapper;


    @Override
    public Result<Integer> isCollectionArticle(CollectionLikesParam param, String userid, boolean isCollection) {
        //验证参数是否正确
        if (StringUtils.isEmpty(param.getArticleId())) {
            return Result.error(ResponseEnum.NO_COMMENT_ID);
        }
        List<ArticleList> articleIds = articleMapper.findArticleByArticleIds(Collections.singletonList(param.getArticleId()));
        if (CollectionUtils.isEmpty(articleIds)) {
            return Result.error(ResponseEnum.NO_ARTICLE_ID);
        }
        //先查询是否已收藏该文章
        String collectionArticleIdsJson = stringRedisTemplate.opsForValue().get(String.format(Constant.COLLECTION_USER_KEY, userid));
        List<String> collectionArticleIds = JSONObject.parseArray(collectionArticleIdsJson, String.class);

        String collectionUserIdsJson = stringRedisTemplate.opsForValue().get(String.format(Constant.COLLECTION_ARTICLE_KEY, param.getArticleId()));
        List<String> collectionUserIds = JSONObject.parseArray(collectionUserIdsJson, String.class);


        if (isCollection) {
            //添加收藏  userId->ArticleId
            if (!CollectionUtils.isEmpty(collectionArticleIds)) {
                if (!collectionArticleIds.contains(param.getArticleId())) {
                    collectionArticleIds.add(param.getArticleId());
                }
                String articleIdJson = JSONObject.toJSONString(collectionArticleIds);
                stringRedisTemplate.opsForValue().set(String.format(Constant.COLLECTION_USER_KEY, userid), articleIdJson);
            } else {
                String articleIdJson = JSONObject.toJSONString(Collections.singletonList(param.getArticleId()));
                stringRedisTemplate.opsForValue().set(String.format(Constant.COLLECTION_USER_KEY, userid), articleIdJson);
            }

            //添加收藏  ArticleId->userId
            if (!CollectionUtils.isEmpty(collectionUserIds)) {
                if (!collectionUserIds.contains(userid)) {
                    collectionUserIds.add(userid);
                }
                String userIdsJson = JSONObject.toJSONString(collectionUserIds);
                stringRedisTemplate.opsForValue().set(String.format(Constant.COLLECTION_ARTICLE_KEY, param.getArticleId()), userIdsJson);
            } else {
                String userIdsJson = JSONObject.toJSONString(Collections.singletonList(userid));
                stringRedisTemplate.opsForValue().set(String.format(Constant.COLLECTION_ARTICLE_KEY, param.getArticleId()), userIdsJson);
            }

            return Result.success(ResponseEnum.COLLECTION_SUCCESS);

        } else {
            //取消收藏 userId->ArticleId
            if (!CollectionUtils.isEmpty(collectionArticleIds)) {
                collectionArticleIds.remove(param.getArticleId());
                if (!CollectionUtils.isEmpty(collectionArticleIds)) {
                    String jsonString = JSONObject.toJSONString(collectionArticleIds);
                    stringRedisTemplate.opsForValue().set(String.format(Constant.COLLECTION_USER_KEY, userid), jsonString);
                } else {
                    stringRedisTemplate.delete(String.format(Constant.COLLECTION_USER_KEY, userid));
                }

            } else {
                return Result.success(ResponseEnum.PARAM_ERROR);
            }

            //取消收藏  ArticleId->userId
            if (!CollectionUtils.isEmpty(collectionUserIds)) {
                collectionUserIds.remove(userid);
                if (!CollectionUtils.isEmpty(collectionUserIds)) {
                    String jsonString = JSONObject.toJSONString(collectionUserIds);
                    stringRedisTemplate.opsForValue().set(String.format(Constant.COLLECTION_ARTICLE_KEY, param.getArticleId()), jsonString);
                } else {
                    stringRedisTemplate.delete(String.format(Constant.COLLECTION_ARTICLE_KEY, param.getArticleId()));
                }

            } else {
                return Result.error(ResponseEnum.PARAM_ERROR);
            }
            return Result.success(ResponseEnum.UN_COLLECTION_SUCCESS);
        }

    }


    @Override
    public Result<Integer> isLikeArticle(CollectionLikesParam param, String userid, boolean isLike) {
        if (StringUtils.isEmpty(param.getArticleId())) {
            return Result.error(ResponseEnum.NO_COMMENT_ID);
        }
        List<ArticleList> articleIds = articleMapper.findArticleByArticleIds(Collections.singletonList(param.getArticleId()));
        if (CollectionUtils.isEmpty(articleIds)) {
            return Result.error(ResponseEnum.NO_ARTICLE_ID);
        }
        //先查询是否已点赞该文章
        //查询用户已点赞的文章
        String collectionArticleIdsJson = stringRedisTemplate.opsForValue().get(String.format(Constant.LIKES_USER_KEY, userid));
        List<String> collectionArticleIds = JSONObject.parseArray(collectionArticleIdsJson, String.class);

        //查询文章点赞的用户
        String collectionUserIdsJson = stringRedisTemplate.opsForValue().get(String.format(Constant.LIKES_ARTICLE_KEY, param.getArticleId()));
        List<String> collectionUserIds = JSONObject.parseArray(collectionUserIdsJson, String.class);


        if (isLike) {
            //添加点赞  userId->ArticleId
            if (!CollectionUtils.isEmpty(collectionArticleIds)) {
                if (!collectionArticleIds.contains(param.getArticleId())) {
                    collectionArticleIds.add(param.getArticleId());
                }
                String articleIdJson = JSONObject.toJSONString(collectionArticleIds);
                stringRedisTemplate.opsForValue().set(String.format(Constant.LIKES_USER_KEY, userid), articleIdJson);
            } else {
                String articleIdJson = JSONObject.toJSONString(Collections.singletonList(param.getArticleId()));
                stringRedisTemplate.opsForValue().set(String.format(Constant.LIKES_USER_KEY, userid), articleIdJson);
            }

            //添加点赞  ArticleId->userId
            if (!CollectionUtils.isEmpty(collectionUserIds)) {
                if (!collectionUserIds.contains(userid)) {
                    collectionUserIds.add(userid);
                }
                String userIdsJson = JSONObject.toJSONString(collectionUserIds);
                stringRedisTemplate.opsForValue().set(String.format(Constant.LIKES_ARTICLE_KEY, param.getArticleId()), userIdsJson);
            } else {
                String userIdsJson = JSONObject.toJSONString(Collections.singletonList(userid));
                stringRedisTemplate.opsForValue().set(String.format(Constant.LIKES_ARTICLE_KEY, param.getArticleId()), userIdsJson);
            }

            return Result.success(ResponseEnum.LIKE_SUCCESS);

        } else {
            //取消点赞 userId->ArticleId
            if (!CollectionUtils.isEmpty(collectionArticleIds)) {
                collectionArticleIds.remove(param.getArticleId());
                if (!CollectionUtils.isEmpty(collectionArticleIds)) {
                    String jsonString = JSONObject.toJSONString(collectionArticleIds);
                    stringRedisTemplate.opsForValue().set(String.format(Constant.LIKES_USER_KEY, userid), jsonString);
                } else {
                    stringRedisTemplate.delete(String.format(Constant.LIKES_USER_KEY, userid));
                }

            } else {
                return Result.success(ResponseEnum.PARAM_ERROR);
            }

            //取消点赞  ArticleId->userId
            if (!CollectionUtils.isEmpty(collectionUserIds)) {
                collectionUserIds.remove(userid);
                if (!CollectionUtils.isEmpty(collectionUserIds)) {
                    String jsonString = JSONObject.toJSONString(collectionUserIds);
                    stringRedisTemplate.opsForValue().set(String.format(Constant.LIKES_ARTICLE_KEY, param.getArticleId()), jsonString);
                } else {
                    stringRedisTemplate.delete(String.format(Constant.LIKES_ARTICLE_KEY, param.getArticleId()));
                }

            } else {
                return Result.error(ResponseEnum.PARAM_ERROR);
            }
            return Result.success(ResponseEnum.UN_LIKE_SUCCESS);
        }
    }

    @Override
    public Result<ArticleCollectionLikesVO> findArticleCollectionLikes(CollectionLikesParam param, Users user) {

        ArticleCollectionLikesVO collectionLikesVO = new ArticleCollectionLikesVO();
        collectionLikesVO.setArticleId(param.getArticleId());

        //查询用户是否收藏
        if (user != null) {
            String collectionArticleIdsJson = stringRedisTemplate.opsForValue().get(String.format(Constant.COLLECTION_USER_KEY, user.getUserid()));
            List<String> collectionArticleIds = JSONObject.parseArray(collectionArticleIdsJson, String.class);
            collectionLikesVO.setCollection(!CollectionUtils.isEmpty(collectionArticleIds) && collectionArticleIds.contains(param.getArticleId()));

            //查询用户是否点赞
            String collectionUserIdsJson = stringRedisTemplate.opsForValue().get(String.format(Constant.LIKES_USER_KEY, user.getUserid()));
            List<String> collectionUserIds = JSONObject.parseArray(collectionUserIdsJson, String.class);
            collectionLikesVO.setLike(!CollectionUtils.isEmpty(collectionUserIds) && collectionUserIds.contains(param.getArticleId()));
        }

        //查询文章总评论量
        List<Comment> comments = commentMapper.findCommentByCommentTargetId(param.getArticleId());
        collectionLikesVO.setCommentCount(comments.size());
        //查询本文章总点赞量
        String articleLikeCounts = stringRedisTemplate.opsForValue().get(String.format(Constant.LIKES_ARTICLE_KEY, param.getArticleId()));
        List<String> collectionArticleIds = JSONObject.parseArray(articleLikeCounts, String.class);

        collectionLikesVO.setLikeCount(!CollectionUtils.isEmpty(collectionArticleIds)? collectionArticleIds.size():0);
        //查询本文章总收藏量
        String articleCollectionCountsJson = stringRedisTemplate.opsForValue().get(String.format(Constant.COLLECTION_ARTICLE_KEY, param.getArticleId()));
        List<String> articleCollectionCounts = JSONObject.parseArray(articleCollectionCountsJson, String.class);
        collectionLikesVO.setCollectionCount(!CollectionUtils.isEmpty(articleCollectionCounts)? articleCollectionCounts.size():0);

        return Result.success(collectionLikesVO);
    }

}
